#include <QCoreApplication>
#include <QDir>

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>


#include <iostream>
#include <vector>

using namespace std;
using namespace cv;


int main(int argc, char *argv[])
{

    cout << "OpenCV Training SVM Automatic Number Plate Recognition\n";
        cout << "\n";
        String path_Plates;
        String path_NoPlates;
        //char* path_Plates;
        //char* path_NoPlates;
        int numPlates;
        int numNoPlates;
        int imageWidth=144;
        int imageHeight=33;

        numPlates = 14;
        numNoPlates = 23;
        path_Plates = '/home/bascht/Korrekt';
        path_NoPlates = '/home/bascht/Fehler';


        //Check if user specify image to process
        /*if(argc >= 5 )
        {
            numPlates= atoi(argv[1]);
            numNoPlates= atoi(argv[2]);
            path_Plates= argv[3];
            path_NoPlates= argv[4];

        }else{
            cout << "Usage:\n" << argv[0] << argv[1]<< " <num Plate Files> <num Non Plate Files> <path to plate folder files> <path to non plate files> \n";
            return 0;
        }*/

        Mat classes;//(numPlates+numNoPlates, 1, CV_32FC1);
        Mat trainingData;//(numPlates+numNoPlates, imageWidth*imageHeight, CV_32FC1 );

        Mat trainingImages;
        vector<int> trainingLabels;

        // korrekte images
        QDir directoryK("/home/bascht/Korrekt");
        QStringList Filter;
        Filter << "*.jpg";

        QStringList flistK = directoryK.entryList(Filter);

        QStringList::const_iterator testK;
        for(testK = flistK.constBegin();testK!= flistK.constEnd(); ++testK)
        {
           std::cout << (*testK).toLatin1().constData() << std::endl;
           std::string filename = std::string("/home/bascht/Korrekt/") + (*testK).toLatin1().constData();
           Mat img=imread(filename, 0);
           img= img.reshape(1, 1);
           trainingImages.push_back(img);
           trainingLabels.push_back(1);
        }

        // fehlerbilder
        QDir directoryF("/home/bascht/Fehler");

        QStringList flist = directoryF.entryList(Filter);

        QStringList::const_iterator testF;
        for(testF = flist.constBegin();testF!= flist.constEnd(); ++testF)
        {
           std::cout << (*testF).toLatin1().constData() << std::endl;
           std::string filename = std::string("/home/bascht/Fehler/") + (*testF).toLatin1().constData();
           Mat img=imread(filename, 0);
           img= img.reshape(1, 1);
           trainingImages.push_back(img);
           trainingLabels.push_back(0);
        }

        Mat(trainingImages).copyTo(trainingData);
        trainingData = trainingData.reshape(1,trainingData.rows);
        trainingData.convertTo(trainingData, CV_32FC1);
        Mat(trainingLabels).copyTo(classes);

        FileStorage fs("SVM.xml", FileStorage::WRITE);
        fs << "TrainingData" << trainingData;
        fs << "classes" << classes;
        fs.release();

        return 0;

}
